﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class WorkBreakdownStructuresViewModel : INotifyPropertyChanged
    {
        private static WorkBreakdownStructuresViewModel mInstance = null;
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<WorkBreakdownStructureGroupViewModel> Groups { get; set; }
        private WorkBreakdownStructureGroupViewModel mGroup = null;

        public WorkBreakdownStructureGroupViewModel Group
        {
            get { return mGroup; }
            set
            {
                mGroup = value;
                RaisePropertyChange("Group");
            }
        }

        private WorkBreakdownStructuresViewModel()
        {
            if (null == Groups) Groups = new ObservableCollection<WorkBreakdownStructureGroupViewModel>();
        }

        public static WorkBreakdownStructuresViewModel GetInstance()
        {
            if (null == mInstance) mInstance = new WorkBreakdownStructuresViewModel();
            return mInstance;
        }

        public WorkBreakdownStructureViewModel FindByCode(string strCode)
        {
            WorkBreakdownStructureViewModel _found = null;
            if (string.IsNullOrEmpty(strCode)) return _found;
            foreach (WorkBreakdownStructureGroupViewModel _group in this.Groups)
            {
                _found = _group.FindByCode(strCode);
                if (null != _found) break;
            }
            return _found;
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (0 < Groups.Count)
            {
                _element = doc.CreateElement("Groups");
                foreach (WorkBreakdownStructureGroupViewModel _group in Groups) _element.AppendChild(_group.Export(doc));
            }
            return _element;
        }

        public void Import(XmlElement element)
        {
            if (null == element) return;
            Groups.Clear();
            if ("Groups" == element.Name)
            {
                if (element.HasChildNodes)
                {
                    foreach (XmlNode _node in element.ChildNodes)
                    {
                        WorkBreakdownStructureGroupViewModel _group = new WorkBreakdownStructureGroupViewModel();
                        _group.Import(_node as XmlElement);
                        Groups.Add(_group);
                    }
                    if (0 < Groups.Count) this.Group = Groups[0];
                }
            }
        }

        private void RaisePropertyChange(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}
